import request from '../../utils/request'
import { Modal } from 'antd'
import url from '../../utils/projectInfo'
export default {
  namespace: 'puzzlecards',
  state: {
    data: [],
    counter: 0,
    pagination: {
      pageNum: 1,
      pageSize: 5,
      totalElements: 0,
      totalPages: 0
    },
    elements: []
  },
  effects: {
    *queryInitCards(_, sagaEffects) {
      const { call, put } = sagaEffects
      const endPointURI = '/dev/random_joke'
      const puzzle = yield call(request, endPointURI)
      yield put({ type: 'addNewCard', payload: puzzle })

      yield call(delay, 3000)

      const puzzle2 = yield call(request, endPointURI)
      yield put({ type: 'addNewCard', payload: puzzle2 })
    },
    *findTableData(action, { call, put }) {
      const result = yield call(
        request,
        `http://localhost:8080/find`,
        'get',
        action.payload
      )
      if (result) {
        const {
          content,
          totalPages,
          totalElements,
          pageable: { pageNumber, pageSize }
        } = result
        console.log(result)
        let pagination = {
          totalPages,
          totalElements,
          pageNum: pageNumber + 1,
          pageSize
        }
        yield put({ type: 'save', payload: { elements: content, pagination } })
      } else {
        Modal.error({ title: '查询失败!' })
      }
    },
    *delete(action, { call, put }) {
      const result = yield call(
        request,
        `http://localhost:8080/deleteById`,
        'get',
        action.payload
      )
      if (result.content) {
        Modal.success({ title: '删除成功!' })
        const {
          content,
          totalPages,
          totalElements,
          pageable: { pageNumber, pageSize }
        } = result
        console.log(result)
        let pagination = {
          totalPages,
          totalElements,
          pageNum: pageNumber + 1,
          pageSize
        }
        yield put({ type: 'save', payload: { elements: content, pagination } })
      } else {
        Modal.error({ title: '删除失败!' })
      }
    },
    *add(action, { call, put }) {
      const result = yield call(
        request,
        `http://localhost:8080/add`,
        'post',
        action.payload
      )
      if (result.content) {
        Modal.success({ title: '新增成功!' })
        const {
          content,
          totalPages,
          totalElements,
          pageable: { pageNumber, pageSize }
        } = result
        console.log(result)
        let pagination = {
          totalPages,
          totalElements,
          pageNum: pageNumber + 1,
          pageSize
        }
        yield put({ type: 'save', payload: { elements: content, pagination } })
      } else {
        Modal.error({ title: '新增失败!' })
      }
    }
  },
  reducers: {
    addNewCard(state, { payload: newCard }) {
      const nextCounter = state.counter + 1
      const newCardWithId = { ...newCard, id: nextCounter }
      const nextData = state.data.concat(newCardWithId)
      return {
        data: nextData,
        counter: nextCounter
      }
    },
    save(state, action) {
      console.log(action.payload)
      return {
        ...state,
        ...action.payload
      }
    }
  }
}
